import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/login/index'
import layout from '@/views/layout/index'
import article from '@/views/article/index'
import dashboard from '@/views/dashboard/index'
import store from '@/store/index.js'
Vue.use(VueRouter)
const routes = [
  // 重定向
  { path: '/', redirect: '/layout' },
  { path: '/login', component: login },
  {
    path: '/layout',
    component: layout,
    children:
      [
        { path: '/', redirect: '/dashboard' },
        { path: '/article', component: article },
        { path: '/dashboard', component: dashboard }]

  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
/* 登录权限  有token || 跳转登录页
*/
router.beforeEach((to, from, next) => {
  if (store.state.user.token || to.path === '/login') {
    next()
  } else {
    next('/login')
  }
})

export default router
